package example.com.zcq.mymj.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import example.com.zcq.mymj.R;

/**
 * @author zcq
 * @package example.com.zcq.mymj.fragment
 * @fileName MyMj
 * @description: 这个类的描述
 * @org 融信通达科技有限公司
 * @email @163.com
 * @date 2018/11/22  19:59
 */
public class FragmentTab4 extends Fragment {


    private WebView webView;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    webView.goBack();
                    break;
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_tab1, container, false);


        initView(view);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.setHorizontalScrollBarEnabled(false);//水平不显示
        webView.setVerticalScrollBarEnabled(false); //垂直不显示
        //設置web自适应
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);

        // 设置可以支持缩放
        webView.getSettings().setSupportZoom(true);
        //扩大比例的缩放
        webView.getSettings().setUseWideViewPort(true);
        String cacheDirPath = getActivity().getFilesDir().getAbsolutePath() + "webcache"; //缓存路径
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);  //缓存模式
        webView.getSettings().setAppCachePath(cacheDirPath); //设置缓存路径
        webView.getSettings().setAppCacheEnabled(true); //开启缓存功能
        webView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
                    if (event.getAction() == KeyEvent.ACTION_DOWN) { //只处理一次
                        handler.sendEmptyMessage(1);
                    }
                    return true;
                }
                return false;
            }
        });
        webView.loadUrl("http://www.readers365.com/xiyouji/index.html");


        return view;
    }


    private void initView(View view) {
        webView = (WebView) view.findViewById(R.id.wv_tab_fg);
    }

    private void destroyWebView() {
        if (webView != null) {
            webView.clearHistory();
            webView.clearCache(true);
            webView.loadUrl("about:blank"); // clearView() should be changed to loadUrl("about:blank"), since clearView() is deprecated now
            webView.freeMemory();
            webView.destroy();
            webView.pauseTimers();
            webView = null; // Note that mWebView.destroy() and mWebView = null do the exact same thing
        }
    }

    @Override
    public void onDestroy() {
        destroyWebView();
        super.onDestroy();
    }

    /**
     * 返回键返回上一网页
     */

}
